﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using LiveChartsCore.Defaults;
using System.Collections.ObjectModel;
using UltrasoundTestingProject.Models;
using UltrasoundTestingProject.SeedDatas;
using Wpf.Ui.Controls;

namespace UltrasoundTestingProject.ViewModels
{
    public class MainWindowViewModel : ObservableRecipient
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public MainWindowViewModel()
        {
            _menus = SeedData.GetMenus();
            _cartesianChartModel = new();
            FlushBtnCommand = new(FlushBtnOnClick);
            LeftBtnCommand = new(LeftBtnOnClick);
            RightBtnCommand = new(RightBtnOnClick);
        }

        #region 字段

        private CartesianChartModel _cartesianChartModel;

        public CartesianChartModel CartesianChartModel
        {
            get { return _cartesianChartModel; }
            set { SetProperty(ref _cartesianChartModel, value); }
        }

        /// <summary>
        /// 菜单集合
        /// </summary>
        private ObservableCollection<NavigationViewItem> _menus;

        public ObservableCollection<NavigationViewItem> Menus
        {
            get { return _menus; }
            set { _menus = value; }
        }

        #endregion

        #region 命令

        /// <summary>
        /// 刷新按钮命令
        /// </summary>
        public RelayCommand FlushBtnCommand { get; set; }

        /// <summary>
        /// 左按钮命令
        /// </summary>
        public RelayCommand LeftBtnCommand { get; set; }

        /// <summary>
        /// 右按钮命令
        /// </summary>
        public RelayCommand RightBtnCommand { get; set; }

        #endregion

        #region 私有方法

        /// <summary>
        /// 刷新按钮事件
        /// </summary>
        private void FlushBtnOnClick()
        {
            _cartesianChartModel.Series[1].Values = _cartesianChartModel.GetSecondAxis();
        }

        /// <summary>
        /// 左按钮点击事件
        /// </summary>
        private void LeftBtnOnClick()
        {
            foreach (var item in _cartesianChartModel.Series[1].Values!.Cast<ObservablePoint>())
            {
                if (item.X == 0)
                {
                    return;
                }
                item.X -= 10;
            }
        }

        /// <summary>
        /// 右按钮点击事件
        /// </summary>
        private void RightBtnOnClick()
        {
            var xMax = Math.Ceiling(_cartesianChartModel.SeriesValues.Count / 50.0) * 50.0;
            foreach (var item in _cartesianChartModel.Series[1].Values!.Cast<ObservablePoint>().OrderByDescending(x => x.X))
            {
                if (item.X == xMax)
                {
                    return;
                }
                item.X += 10;
            }
        }

        #endregion

        #region 消息订阅与发送

        

        #endregion
    }
}


